

package com.hazelcast.internal.util;

import java.util.Properties;

/**
 * Utility for checking runtime Java VM.
 */
public enum JavaVm {
    UNKNOWN, HOTSPOT, OPENJ9;

    public static final JavaVm CURRENT_VM = detectCurrentVM();

    private static JavaVm detectCurrentVM() {
        Properties properties = System.getProperties();
        return parse(properties);
    }

    static JavaVm parse(Properties properties) {
        String vmName = properties.getProperty("java.vm.name");
        if (vmName == null) {
            return UNKNOWN;
        }
        // Oracle JDK 'java.vm.name': Java HotSpot(TM) 64-Bit Server VM
        // Oracle JDK 'sun.management.compiler': HotSpot 64-Bit Tiered Compilers
        if (vmName.contains("HotSpot")) {
            return HOTSPOT;
        }
        // Eclipse OpenJ9 'java.vm.name': Eclipse OpenJ9 VM
        if (vmName.contains("OpenJ9")) {
            return OPENJ9;
        }
        String prop = properties.getProperty("sun.management.compiler");
        if (prop == null) {
            return UNKNOWN;
        }
        // HotSpot based OpenJDK builds 'java.vm.name': OpenJDK 64-Bit Server VM.
        // HotSpot based OpenJDK builds 'sun.management.compiler': HotSpot 64-Bit Tiered Compilers
        if (prop.contains("HotSpot")) {
            return HOTSPOT;
        }
        return UNKNOWN;
    }
}
